package model

import (
	"fmt"

	"fiber-demo/internal/global"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func InitDatabase() error {
	conf := global.Config.Database
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		conf.Username,
		conf.Password,
		conf.Host,
		conf.Port,
		conf.DB,
	)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	global.DB = db
	return err
}
